package com.doubi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doubi.entity.Friend;
import com.doubi.entity.Releasetable;
import com.doubi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RestController
@CrossOrigin("*")
public class ReleaseDataController {
    @Autowired
    ReleasetableService releasetableService;
    @Autowired
    HistoryService historyService;
    @Autowired
    LiketableService liketableService;
    @Autowired
    ForwardService forwardService;
    @Autowired
    FriendService friendService;

    @GetMapping("myReleaseData")
    public Map<String, Object> myReleaseData(Integer userid) {
        Map<String, Object> result = new HashMap<>();
        //我的作品近七天播放量
        LocalDate now = LocalDate.now();
        //计算七天前的日期
        LocalDate oldDay = now.plusDays(-7L);
        //计算十四天前的日期
        LocalDate ollldDay = now.plusDays(-14L);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //查询我七天前的作品数据变化
        //1.查询我的所有作品集合的id
        QueryWrapper<Releasetable> qw1=new QueryWrapper<>();
        qw1.eq("userId",userid);
        List<Integer> list=new ArrayList<>();
        releasetableService.list(qw1).forEach(releasetable -> {
            list.add(releasetable.getReleaseId());
        });
        if(list.size()>0) {
            //统计近七天我的播放量
            Integer data1 = historyService.recentlyHistory(list, oldDay.toString(),null);
            result.put("sevenAgeView",data1);
            //统计较上个七天的播放量
            Integer data2 = historyService.recentlyHistory(list, oldDay.toString(),ollldDay.toString());
            result.put("fourteenAgeView",data2);
            //点赞
            Integer likeNum1 = liketableService.recentlyLike(list, oldDay.toString(), null);
            result.put("sevenAgeLike",likeNum1);
            Integer likeNum2 = liketableService.recentlyLike(list, oldDay.toString(), ollldDay.toString());
            result.put("fourteenAgeLike",likeNum2);
            //分享
            Integer forwardNum1 = forwardService.recentlyForward(list, oldDay.toString(), null);
            result.put("sevenAgeForward",forwardNum1);
            Integer forwardNum2 = forwardService.recentlyForward(list, oldDay.toString(), ollldDay.toString());
            result.put("fourteenAgeForward",forwardNum2);
            //总粉丝数
            QueryWrapper<Friend> qw2=new QueryWrapper<>();
            qw2.eq("userFriend",userid);
            Integer fansCount = friendService.list(qw2).size();
            result.put("fansCount",fansCount);
            //7天播放量曲线图数据
            int[] historyCurve=new int[7];
            int[] forwardCurve=new int[7];
            int[] likeCurve=new int[7];
            String[] dateTime=new String[7];
            for (int i=0;i<7;i++) {
                int hNum=historyService.todayLookNum(list,now.plusDays(-i).toString());
                int fNum=forwardService.todayForwardNum(list,now.plusDays(-i).toString());
                int lNum=forwardService.todayForwardNum(list,now.plusDays(-i).toString());
                historyCurve[i]=hNum;
                forwardCurve[i]=fNum;
                likeCurve[i]=lNum;
                dateTime[i]=now.plusDays(-(6-i)).toString().substring(5);
            }
            result.put("historyCurve",historyCurve);
            result.put("forwardCurve",forwardCurve);
            result.put("likeCurve",likeCurve);
            result.put("dataTime",dateTime);
        }else {
            result.put("sevenAgeView",0);
            result.put("fourteenAgeView",0);
            result.put("sevenAgeLike",0);
            result.put("fourteenAgeLike",0);
            result.put("sevenAgeForward",0);
            result.put("fourteenAgeForward",0);
            result.put("historyCurve",new int[7]);
            result.put("forwardCurve",new int[7]);
            result.put("likeCurve",new int[7]);
            String[] dateTime=new String[7];
            for (int i=0;i<7;i++) {
                dateTime[i]=now.plusDays(-(6-i)).toString().substring(5);
            }
            result.put("dataTime",dateTime);
            //总粉丝数
            QueryWrapper<Friend> qw2=new QueryWrapper<>();
            qw2.eq("userFriend",userid);
            Integer fansCount = friendService.list(qw2).size();
            result.put("fansCount",fansCount);
        }
        return result;
    }
}
